package DAO;

import java.awt.Font;
import java.awt.event.*;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.*;
import javax.xml.crypto.dsig.keyinfo.KeyValue;

import com.mysql.jdbc.EscapeTokenizer;

import Main.DBMain;
import View.QueryFrame;

public class QueryByName extends JFrame {
	private JPanel contentPane = new JPanel();
	private JTextField stuNameField = new JTextField();
	public static JTextArea showArea = new JTextArea(100,100);
	private JLabel lab_Tip;
	private JButton btn_Exit;
	private JButton btn_Query;	
	String stuName ;
	
	public QueryByName() {
		setTitle("个人成绩");
		setSize(600, 200);
		setLocationRelativeTo(null);
		setContentPane(contentPane);
		contentPane.setLayout(null);

		lab_Tip = new JLabel("请输入你的姓名:");
		lab_Tip.setFont(new Font("宋体", Font.PLAIN, 16));;
		lab_Tip.setBounds(30, 10,150, 40);
		
		/*按键监听，回车查询*/
		stuNameField.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
			if (e.getKeyCode()==KeyEvent.VK_ENTER)
				try {
					query();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		});
		/*鼠标动作监听，点击文本框清空内容*/
		stuNameField.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				stuNameField.setText("");
			}
		});
		stuNameField.setBounds(180, 15, 130, 30);		
		
		btn_Query=new JButton("查询");
		btn_Query.setFont(new Font("宋体", Font.BOLD, 16));;
		btn_Query.setBounds(360, 10, 70, 40);
		/*按钮监听，点击查询*/
		btn_Query.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					query();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		});
		
		showArea.setText("");
		showArea.setBounds(5,65, 575, 90);
		
		btn_Exit = new JButton("关闭");
		btn_Exit.setFont(new Font("宋体", Font.BOLD, 16));;
		btn_Exit.setBounds(480, 10, 70, 40);
		btn_Exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new QueryFrame();
				setVisible(false);
			}
		});
		
		contentPane.add(lab_Tip);
		contentPane.add(stuNameField);
		contentPane.add(showArea);
		contentPane.add(btn_Query);
		contentPane.add(btn_Exit);
		
		setVisible(true);
	}
	public void query() throws SQLException{
		java.sql.Connection conn=null ;
		PreparedStatement pstmt ;
		ResultSet rs ;
		stuName=stuNameField.getText().trim();
		showArea.setText("");
		/*输入的姓名不为空，进行查询*/
		if (!("".equals(stuName))) {
			try {
				conn = DBMain.getConnection();
				String sql = "select * from stu_info where stuName= '"+stuName+"'";//查询语句
				pstmt = conn.prepareStatement(sql);
				rs = pstmt.executeQuery(sql);
				ResultSetMetaData colName=rs.getMetaData();
				/*获取查询到的对象*/
				if (rs.next()) {
					for (int i = 1; i < colName.getColumnCount(); i++) {
						showArea.append(colName.getColumnName(i) + "\t");
					}
					showArea.append("\n");
					for (int i = 1; i < colName.getColumnCount(); i++) {
						showArea.append(rs.getString(i) + "\t");
					}
					showArea.append("\n");
				}else {
					JOptionPane.showMessageDialog(null, "该学生不存在！请重新输入");
					stuNameField.setText("");
				}
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		} else {
			JOptionPane.showMessageDialog(null, "请输入姓名！");
		}
	}
}
